package com.ibt.controller;

import javax.mail.MessagingException;

import javax.mail.internet.AddressException;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ibt.dao.EmployeeDAO;
import com.ibt.entity.Business;
import com.ibt.entity.Employee;
import com.ibt.entity.Login;
import com.ibt.util.SendMail;
@Controller
@RequestMapping("/newuser")
public class NewUserController {

		@RequestMapping(value="/add.do",method = RequestMethod.POST)
		public ModelAndView userEntry(@RequestParam("uname") String username, 
				@RequestParam("empcode") String empcode,
				@RequestParam("email") String email, 
				@RequestParam("selectrole") String role,
				ModelAndView model,HttpSession session) {
			Business bus=(Business)session.getAttribute("business");
				Login  Ulogin=(Login)session.getAttribute("user");
				
			int busId=bus.getBusinessId();
			Employee emp=new Employee(empcode,username,email,role,bus);
         try {
			EmployeeDAO.insert(emp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendMail sm=new SendMail();
		 String mail=email;//"akhilchoudary03@gmail.com";//emp.getEmail();
		 
		 String from="bugtracker.mtech@gmail.com";
			System.out.println("User Email Id Is:"+from);
			String pass="btrack123";
			System.out.println("User password Is:"+pass);
			System.out.println("Email is:"+from+" Password is:"+pass);
			String soEmail="rajakmca09@gmail.com";
		
			
			StringBuilder msgBody= new StringBuilder(); 
			msgBody.append("I have created a user for you in Intelligent Bugtracker <br/><br/> \n")
			.append("Your userId:"+mail+"</br>\n")
			.append("To set your password,.<br/> \n")
			.append("go to:"+"http://impbtrack.impbtrack.cloudbees.net/jsp/setpassword.jsp?user="+mail+"<br/>\n\n\n")
			.append(".............................\n")
			.append(" Team Manager: <br/>\n \n ")
			.append(Ulogin.getUserName()+" <br/> \n ");
			
			try {
				sm.sendMailNoFile(from,pass,msgBody.toString(), "Use This Link To set Password", mail);
			} catch (AddressException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (MessagingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.out.println("in x0029 searching  BusinessUserid " );
			model.setViewName("/jsp/userentry.jsp");
			model.addObject("msg","New User is Added to Your Company with EmpName is:"+username);
			return model;

}
}